package com.hjy.service;

import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.hjy.beans.LogType;
import com.hjy.common.RequestHolder;
import com.hjy.mapper.SysLogMapper;
import com.hjy.mapper.SysRoleUserMapper;
import com.hjy.mapper.SysUserMapper;
import com.hjy.pojo.SysLogWithBLOBs;
import com.hjy.pojo.SysRoleUser;
import com.hjy.pojo.SysUser;
import com.hjy.util.IpUtil;
import com.hjy.util.JsonMapper;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.stream.Collector;
import java.util.stream.Collectors;

@Service
public class SysRoleUserService {
    @Resource
    private SysRoleUserMapper sysRoleUserMapper;
    @Resource
    private SysUserMapper sysUserMapper;
    @Resource
    private SysLogMapper sysLogMapper;

    public List<SysUser> getListByRoleId(int roleId) {

        List<Integer> userIdList = sysRoleUserMapper.getUserIdListByRoleId(roleId);
        if (CollectionUtils.isEmpty(userIdList)) {
            return Lists.newArrayList();
        }
        return sysUserMapper.selectBatchIds(userIdList);
    }

    public void changeRoleUsers(int roleId, List<Integer> userIdList, HttpServletRequest request) {
//        .stream().map(sysRoleUser -> sysRoleUser.getUserId()).collect(Collectors.toList())
        List<Integer> originUserIdList = sysRoleUserMapper.getUserIdListByRoleId(roleId);
        if (originUserIdList.size() == userIdList.size()) {
            Set<Integer> originUserIdSet = Sets.newHashSet(originUserIdList);
            Set<Integer> userIdSet = Sets.newHashSet(userIdList);
            originUserIdSet.removeAll(userIdSet);
            if (CollectionUtils.isEmpty(originUserIdSet)) {
                return;
            }
        }
        updateRoleUsers(roleId, userIdList,request);
        saveRoleUserLog(roleId, originUserIdList, userIdList);
    }

    @Transactional
    void updateRoleUsers(int roleId, List<Integer> userIdList, HttpServletRequest request) {
        sysRoleUserMapper.deleteById(roleId);

        if (CollectionUtils.isEmpty(userIdList)) {
            return;
        }
        SysUser user = (SysUser)request.getSession().getAttribute("user");
        for (Integer userId : userIdList) {
            SysRoleUser roleUser = SysRoleUser.builder().roleId(roleId).userId(userId).operator(user.getUsername())
                    .operateIp(IpUtil.getRemoteIp(request)).operateTime(new Date()).build();
            sysRoleUserMapper.insert(roleUser);
        }
    }

    private void saveRoleUserLog(int roleId, List<Integer> before, List<Integer> after) {
        SysLogWithBLOBs sysLog = new SysLogWithBLOBs();
        sysLog.setType(LogType.TYPE_ROLE_USER);
        sysLog.setTargetId(roleId);
        sysLog.setOldValue(before == null ? "" : JsonMapper.obj2String(before));
        sysLog.setNewValue(after == null ? "" : JsonMapper.obj2String(after));
        sysLog.setOperator(RequestHolder.getCurrentUser().getUsername());
        sysLog.setOperateIp(IpUtil.getRemoteIp(RequestHolder.getCurrentRequest()));
        sysLog.setOperateTime(new Date());
        sysLog.setStatus(1);
        sysLogMapper.insert(sysLog);
    }

}
